//
//  JYJSortContentCollectionViewCellTwo.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/4.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJSortContentCollectionViewCellTwo.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "Masonry.h"
#import "JYJTwoSegmentCollectionViewCell.h"
#import "JYJSortContentTitleModel.h"
@interface JYJSortContentCollectionViewCellTwo ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UISegmentedControl *segment;
@end
@implementation JYJSortContentCollectionViewCellTwo
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configViews];
    }
    return self;
}
- (void)setTitleModel:(JYJSortContentTitleModel *)titleModel {
    _titleModel = titleModel;
    [self.collection reloadData];
}
- (void)configViews {
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"最新",@"最热"]];
    [self.contentView addSubview:_segment];
    self.segment.selectedSegmentIndex = 0;
    self.segment.tintColor = [UIColor colorWithRed:0.53 green:0.56 blue:0.62 alpha:1.00];
     [_segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22],NSForegroundColorAttributeName:[UIColor whiteColor]} forState: UIControlStateFocused];
    [self.segment addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.minimumLineSpacing = 0;
    self.collection = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.pagingEnabled = YES;
    [self.contentView addSubview:_collection];
    [self.collection registerClass:[JYJTwoSegmentCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 55);
    self.collection.frame = CGRectMake(0, 55, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 55);
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(80);
        make.right.equalTo(self.contentView.mas_right).with.offset(-80);
        make.height.mas_equalTo(35);
    }];
    self.segment.layer.cornerRadius = 20;
    self.segment.layer.masksToBounds = YES;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJTwoSegmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.handleAction = self.handleAction;
    cell.titleModel = self.titleModel;
    cell.index = indexPath.item;
    if (indexPath.item == 0) {
        self.segment.selectedSegmentIndex = 0;
    }else {
        self.segment.selectedSegmentIndex = 1;
    }
    return cell;
}
- (void)changeValue:(UISegmentedControl *)seg {
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.collection.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.collection.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
            break;

        default:
            break;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
